package com.toKnow.controller;


import com.toKnow.model.entity.Article;
import com.toKnow.model.entity.Source;
import com.toKnow.model.vo.ArticleAbstractVo;
import com.toKnow.model.vo.BasicVo;
import com.toKnow.model.vo.RestrictionVo;
import com.toKnow.model.vo.SourceAbstractVo;
import com.toKnow.service.ArticleService;
import com.toKnow.service.CreatorService;
import com.toKnow.service.SourceService;
import com.toKnow.utils.JwtUserSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/creator/manage")
public class CreatorManageController {
  @Autowired
  CreatorService creatorService;
  @Autowired
  ArticleService articleService;
  @Autowired
  SourceService sourceService;


  public Integer getUid(HttpSession session){
    try{
      String token = (String) session.getAttribute("userToken");
      return Integer.parseInt(JwtUserSessionUtil.getInfo(token,"id"));
    } catch (Exception e){
      return null;
    }
  }

  @ResponseBody
  @RequestMapping(value = "article/draft")
  public BasicVo getArticleDraft(HttpSession session) {
    Integer uid = getUid(session);
    if(uid == null) {
      return new BasicVo(false, "请先登录", null);
    }
    List<ArticleAbstractVo> list = new ArrayList<>();
    List<Article> articles = articleService.getDraftArticle(uid);
    for(Article article : articles) {
      list.add(new ArticleAbstractVo(article));
    }
    return new BasicVo(true, null, list);
  }

  @ResponseBody
  @RequestMapping(value = "article/checking")
  public BasicVo getArticleChecking(HttpSession session) {
    Integer uid = getUid(session);
    if(uid == null) {
      return new BasicVo(false, "请先登录", null);
    }
    List<ArticleAbstractVo> list = new ArrayList<>();
    List<Article> articles = articleService.getCheckingArticle(uid);
    for(Article article : articles) {
      list.add(new ArticleAbstractVo(article));
    }
    return new BasicVo(true, null, list);
  }

  @ResponseBody
  @RequestMapping(value = "article/published")
  public BasicVo getArticlePublished(HttpSession session) {
    Integer uid = getUid(session);
    if(uid == null) {
      return new BasicVo(false, "请先登录", null);
    }
    List<ArticleAbstractVo> list = new ArrayList<>();
    RestrictionVo restrictionVo = new RestrictionVo();
    restrictionVo.setAuthor_id(uid);
    restrictionVo.setSort(2);
    List<Article> articles = articleService.searchArticle(null, restrictionVo);
    for(Article article : articles) {
      list.add(new ArticleAbstractVo(article));
    }
    return new BasicVo(true, null, list);
  }

  @ResponseBody
  @RequestMapping(value = "source/draft")
  public BasicVo getSourceDraft(HttpSession session) {
    Integer uid = getUid(session);
    if(uid == null) {
      return new BasicVo(false, "请先登录", null);
    }
    List<SourceAbstractVo> list = new ArrayList<>();
    List<Source> sources = sourceService.getDraftSource(uid);
    for(Source source : sources) {
      list.add(new SourceAbstractVo(source));
    }
    return new BasicVo(true, null, list);
  }

  @ResponseBody
  @RequestMapping(value = "source/checking")
  public BasicVo getSourceChecking(HttpSession session) {
    Integer uid = getUid(session);
    if(uid == null) {
      return new BasicVo(false, "请先登录", null);
    }
    List<SourceAbstractVo> list = new ArrayList<>();
    List<Source> sources = sourceService.getCheckingSource(uid);
    for(Source source : sources) {
      list.add(new SourceAbstractVo(source));
    }
    return new BasicVo(true, null, list);
  }

  @ResponseBody
  @RequestMapping(value = "source/published")
  public BasicVo getSourcePublished(HttpSession session) {
    Integer uid = getUid(session);
    if(uid == null) {
      return new BasicVo(false, "请先登录", null);
    }
    List<SourceAbstractVo> list = new ArrayList<>();
    RestrictionVo restrictionVo = new RestrictionVo();
    restrictionVo.setAuthor_id(uid);
    restrictionVo.setSort(2);
    List<Source> sources = sourceService.searchSource(null, restrictionVo);
    for(Source source : sources) {
      list.add(new SourceAbstractVo(source));
    }
    return new BasicVo(true, null, list);
  }
}
